/**
 * @file 产品发布
 * @author zhanlong
 */
'use strict';

/* eslint-disable */
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const appDomain = require('./src/config').appDomain;
const lessToJs = require('less-vars-to-js');
const variable = lessToJs(fs.readFileSync(path.join(__dirname, 'src/assets/theme/variable.less'), 'utf8'));

module.exports.getConfig = function (environment, timestamp) {
    const webpackconfig = {
        devtool: 'hidden-source-map',
        entry: {
            index: ['babel-polyfill', './src/entry.js']// entry point for the client app,
        },
        output: {
            path: path.join(__dirname, `./dist/${timestamp}`),
            filename: '[name].[chunkhash:8].js',
            publicPath: environment === 'production'
                        ? `${appDomain}/${timestamp}/`
                        : `/${timestamp}/`,
            chunkFilename: '[id].[chunkhash:8].js'
        },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require(`./dist/${timestamp}/vendor-manifest.json`)
            }),
            new ExtractTextPlugin({
                filename: '[name].[contenthash:8].css',
                disable: false,
                allChunks: true
            }),
            new webpack.DefinePlugin({
                __DEV__: 'false',
                'process.env': {
                    NODE_ENV: JSON.stringify(environment)
                }
            }),
            new WebpackMd5Hash(),
            new HtmlWebpackPlugin({
                client: 'pc',
                filename: '../../views/index.app.ejs',
                template: './plugins/raw-replace-loader?{}!views/index.pc.tmp.ejs'
            }),
            new webpack.optimize.UglifyJsPlugin({
                beautify: false,
                comments: false,
                compress: {
                    warnings: false,
                    drop_console: true,
                    collapse_vars: true,
                    reduce_vars: true
                }
            }),
            new webpack.optimize.ModuleConcatenationPlugin()
        ],
        resolve: {
            alias: {},
            extensions: ['.js', '.jsx', 'es6', 'es']
        },

        module: {
            rules: [
                {
                    test: /src\/containers\/.*\.bundle\.jsx?$/,
                    loaders: 'bundle-loader',
                    query: {
                        lazy: true,
                        name: '[name]'
                    }
                },
                {
                    test: /\.jsx?$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/,
                    query: {
                        presets: [
                            ['es2015', {modules: false, loose: true}],
                            ['react'],
                            ['stage-2']
                        ]
                    }
                },
                {
                    test: path.resolve('./src/redux/reducers'),
                    loader: './plugins/reducer-inject-loader',
                    query: {
                        include: './src',
                        getReducerName(filePath) {
                            const reg = /^.*?containers\/(.*?)\/redux/;
                            const matches = filePath.toLowerCase().match(reg);

                            return matches ? matches[1] : filePath.replace(/[/.]/g, '_');
                        }
                    }
                },
                {
                    test: /\.css$/,
                    exclude: /node_modules|assets/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: 'css-loader?modules&camelCase&localIdentName=[name]__[local]-[hash:base64:5]&sourceMap=true!postcss-loader'
                    })
                },
                {
                    test: /\.css$/,
                    include: /assets|node_modules/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: [
                            {
                                loader: './plugins/css-replace-loader',
                                options: {
                                    alias: {
                                        'https://at.alicdn.com/t/font_zck90zmlh7hf47vi': 'https://iot-static.cdn.bcebos.com/static/font/iconfont'
                                    }
                                }
                            },
                            {
                                loader: 'css-loader',
                                options: {
                                    minimize: true
                                }
                            }
                        ]
                    })
                },
                {
                    test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
                    loader: 'url-loader?limit=500&name=/[name]-[hash].[ext]'
                },
                {
                    test: /\.less/,
                    loader: `style-loader!css-loader!less-loader?{"javascriptEnabled":true,sourceMap: true,"modifyVars":${JSON.stringify(variable)}}`
                },
                {
                    test: /\.json$/,
                    loader: 'json-loader'
                }
            ]
        }
    };

    return webpackconfig;
};

